package com.ruoyi.mq.service.serviceImpl.Processor;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.mq.service.MessageProcessor;
import com.ruoyi.system.domain.GoodsModificationCount;
import com.ruoyi.system.service.IGoodsModificationCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GoodsModificationCountInfoProcessor implements MessageProcessor {
    @Autowired
    private  IGoodsModificationCountService goodsModificationCountService;
    @Override
    public void processMessage(JSONObject message) {
        // 处理商品修改信息
        String tag = message.getString("tag");
        // 处理商品修改信息
        String data = message.getString("data");
        // 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.printf(String.valueOf(message));

        String niot = message.getString("niot");
       List<GoodsModificationCount> goodsModificationCounts = null;
        try {
          goodsModificationCounts = objectMapper.readValue(data, new TypeReference<List<GoodsModificationCount>>() {});
            for (GoodsModificationCount goodsModificationCount : goodsModificationCounts) {
                goodsModificationCountService.insertGoodsModificationCount(goodsModificationCount);
            }


        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }


    }
}
